# Copyright (C) 2015-2024, Wazuh Inc.
# Created by Wazuh, Inc. <info@wazuh.com>.
# This program is free software; you can redistribute it and/or modify it under the terms of GPLv2
import os
import json

from copy import deepcopy


def update_feed_path_configurations(configurations, metadata, feeds_path):
    """Replace feed path tags in the configuration template, using the metadata information.

    Args:
        configurations (list(dict)): List of configuration templates.
        metadata (list(dict)): List of configuration templates metadata.
        feeds_path (str): Absolute path where the feeds are located.

    Returns:
        list(dict): List of configurations with the feeds path updated.
    """
    new_configurations = deepcopy(configurations)

    for index, _ in enumerate(configurations):
        if 'json_feed' in metadata[index] and metadata[index]['json_feed'] is not None:
            new_configurations[index] = json.loads(json.dumps(new_configurations[index]).
                                                   replace(metadata[index]['json_feed_tag'],
                                                   os.path.join(feeds_path, metadata[index]['provider_name'],
                                                                metadata[index]['json_feed'])))

        if 'oval_feed' in metadata[index] and metadata[index]['oval_feed'] is not None:
            new_configurations[index] = json.loads(json.dumps(new_configurations[index]).
                                                   replace(metadata[index]['oval_feed_tag'],
                                                   os.path.join(feeds_path, metadata[index]['provider_name'],
                                                                metadata[index]['oval_feed'])))

        if 'nvd_feed_tag' in metadata[index] and 'nvd_feed' in metadata[index]:
            new_configurations[index] = json.loads(json.dumps(new_configurations[index]).
                                                   replace(metadata[index]['nvd_feed_tag'],
                                                   os.path.join(feeds_path, 'nvd', metadata[index]['nvd_feed'])))

    return new_configurations
